<?php
	$pagination_url = Router::getParam('pass');
	
	$category_index = array(
		'title' =>
			'<div class="head">' .
				'<h1>' . 
				sprintf(
					__d('lil_blogs', 'Categories for "%s"', true),
					$this->Html->clean($admin_area['Area']['name'])
				).
				'</h1>'.
			'</div>',
		'actions' => array(
			'pre' => '<div>',
			'post' => '</div>',
			'lines' => array(
			)
		),
		'table' => array(
			'pre' => '<div class="index">' . PHP_EOL,
			'post' =>
				'</div>' . 
				$this->Html->link(
					__d('lil_blogs', 'Create a new Category', true),
					array(
						'admin' => true,
						'action' => 'add',
						'blog' => (empty($this->params['named']['blog'])) ? null : $this->params['named']['blog']
					)
				),
			'element' => array(
				'parameters' => array(
					'cellspacing' => 0,
					'cellpadding' => 0,
					'id'          => 'AdminAuthorsIndex'
				),
			)
		)
	);

	$category_index['table']['element']['head'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html'       => __d('lil_blogs', 'ID', true),
					),
					'name' => array(
						'parameters' => array('class' => 'left'),
						'html'       => __d('lil_blogs', 'Name', true),
					),
					'edit' => array(
						'parameters' => array(),
						'html'       => '&nbsp;',
					),
					'delete' => array(
						'parameters' => array(),
						'html'       => '&nbsp;',
					),
				)
			)
		)
	);
	
	// table body values
	$category_index['table']['element']['body']['rows'] = array();
	if (empty($data)) {
		$category_index['table']['element']['body']['rows'][0] = array(
			'parameters' => array(),
			'columns' => array(
				0 => array(
					'parameters' => array('colspan' => '4', 'class' => 'light'),
					'html' => __d('lil_blogs', 'No categories that match your criteria.', true)
				)
			),
		);
	} else {
	
		// start with an empty $right stack
		$i = 0; $right = array(); 
		foreach($data as $item) {
			if (empty($item['Category']['parent_id'])) $right = array();
			// only check stack if there is one
			if (count($right)>0) {
				// check if we should remove a node from the stack
				while ($right[count($right) - 1] < $item['Category']['rght']) {
					array_pop($right);
				}
			}
			
			// display indented node title
			$level = count($right);
			
			// add this node to the stack
			$right[] = $item['Category']['rght'];
			
			$category_index['table']['element']['body']['rows'][$i] = array(
				'post' => $item,
				'parameters' => ($i++ % 2 == 0) ? array() : array('class' => 'altrow'),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html' => $item['Category']['id']
					),
					'name' => array(
						'parameters' => array(
							'class' => 'big' . (($level == 0) ? ' strong' : ''),
							'style' => 'padding-left: ' . ($level + 1) * 12 . 'px;'
						),
						'html' => $this->Html->clean($item['Category']['name'])
					),
					'edit' => array(
						'parameters' => array('class' => 'center'),
						'html' => $this->Html->link(
							$this->Html->image(
								'/lil/img/edit.gif', 
								array('alt' => __d('lil_blogs', 'Edit', true))
							),
							array(
								'admin'  => true,
								'action' => 'edit',
								$item['Category']['id']
							),
							array(
								'title' => __d('lil_blogs', 'Edit', true),
								'escape' => false
							)
						)
					),
					'delete' => array(
						'parameters' => array('class' => 'center'),
						'html' => $this->Html->link(
							$this->Html->image('/lil/img/delete.gif',
								array('alt'=>__d('lil_blogs', 'Delete', true))
							),
							array(
								'admin'  => true,
								'action' => 'delete',
								$item['Category']['id']
							),
							array(
								'title' => __d('lil_blogs', 'Delete', true),
								'confirm' => sprintf(__d('lil_blogs', 'Are you sure you want to delete \'%s\'?', true), $item['Category']['name']),
								'escape' => false
							)
						)
					),
				)
			);
			

		}
	}
	
	$category_index['table']['element']['foot'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'paginator' => array(
						'parameters' => array('colspan' => '4', 'class' => 'paging'),
						'html' => $this->Lil->paginator($pagination_url)
					),
				)
			)
		)
	);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// call plugin handlers
	$category_index = $this->callPluginHandlers('admin_index_categories', $category_index);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// form display begins
	echo $category_index['title'] . PHP_EOL;
	
	echo $category_index['actions']['pre'];
	foreach ($category_index['actions']['lines'] as $name => $line) {
		if (is_array($line) && !empty($line['class'])) {
			$parameters = array();
			if (!empty($line['parameters'])) {
				$parameters = (array)$line['parameters'];
			}
			echo call_user_func_array(array($line['class'], $line['method']), $parameters);
		} else {
			echo $line;
		}
	}
	echo $category_index['actions']['post'] . PHP_EOL;
	
	echo $category_index['table']['pre'];
	echo '<table';
	foreach ($category_index['table']['element']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	// display thead
	echo '<thead';
	foreach ($category_index['table']['element']['head']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	foreach ($category_index['table']['element']['head']['rows'] as $row) {
			echo '<tr';
			foreach ($row['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			
			foreach ($row['columns'] as $col) {
				echo '<th';
				foreach ($col['parameters'] as $key => $param) {
					echo ' ' . $key . '="' . $param . '"';
				}
				echo '>' . PHP_EOL;
				
				echo $col['html'];
				echo '</th>' . PHP_EOL;
			}
			
			echo '</tr>' . PHP_EOL;
	}
	echo '</thead>' . PHP_EOL;
	
	// display body
	foreach ($category_index['table']['element']['body']['rows'] as $row) {
		echo '<tr';
		foreach ($row['parameters'] as $key => $param) {
			echo ' ' . $key . '="' . $param . '"';
		}
		echo '>' . PHP_EOL;
		
		foreach ($row['columns'] as $col) {
			echo '<td';
			foreach ($col['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			echo $col['html'];
			echo '</td>' . PHP_EOL;
		}
		
		echo '</tr>' . PHP_EOL;
	}
	
	// display tfoot
	echo '<tfoot';
	foreach ($category_index['table']['element']['foot']['parameters'] as $key => $param) {
		echo ' ' . $key . '="' . $param . '"';
	}
	echo '>' . PHP_EOL;
	
	foreach ($category_index['table']['element']['foot']['rows'] as $row) {
			echo '<tr';
			foreach ($row['parameters'] as $key => $param) {
				echo ' ' . $key . '="' . $param . '"';
			}
			echo '>' . PHP_EOL;
			
			foreach ($row['columns'] as $col) {
				echo '<td';
				foreach ($col['parameters'] as $key => $param) {
					echo ' ' . $key . '="' . $param . '"';
				}
				echo '>' . PHP_EOL;
				echo $col['html'];
				echo '</td>' . PHP_EOL;
			}
			
			echo '</tr>' . PHP_EOL;
	}
	echo '</tfoot>' . PHP_EOL;
	
	echo '</table>' . PHP_EOL;
	echo $category_index['table']['post'];
?>